package ua.kharkov.khpi.tarasenko.bach.web.command.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.khpi.tarasenko.bach.core.Constant;
import ua.kharkov.khpi.tarasenko.bach.core.user.UserBean;
import ua.kharkov.khpi.tarasenko.bach.web.command.Command;

/**
 * Command to log out user.
 * 
 * @author oleksandr
 * 
 */
public class LogOutCommand extends Command {
	private static final Logger log = Logger.getLogger(LogOutCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {

		String forward = Constant.PAGE_LOG_IN;

		HttpSession session = request.getSession();
		if (session != null) {
			UserBean userBean = (UserBean) session
					.getAttribute(Constant.ATTRIBUTE_USER_BEAN);
			log.info("User with id=" + userBean.getUserId() + " logged out.");
			session.invalidate();
		}

		if (log.isDebugEnabled()) {
			log.debug("Forward to " + forward);
		}
		return forward;
	}
}
